﻿using CleanArchitecture.Core;
using CleanArchitecture.Core.Handlers;
using CleanArchitecture.Core.Helper;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Infrastructure.AutoMapper;
using CleanArchitecture.Infrastructure.Entities;
using CleanArchitecture.Infrastructure.Entities.AdminUsers;
using CleanArchitecture.Infrastructure.Entities.Business;
using CleanArchitecture.Infrastructure.Entities.Settings;
using CleanArchitecture.Infrastructure.Entities.System.DataDic;
using CleanArchitecture.Infrastructure.Entities.SystemModules;
using CleanArchitecture.Infrastructure.Interfaces;
using CleanArchitecture.Infrastructure.Interfaces.AdminUsers;
using CleanArchitecture.Infrastructure.Interfaces.Business;
using CleanArchitecture.Infrastructure.Interfaces.Security;
using CleanArchitecture.Infrastructure.Interfaces.SystemManage;
using CleanArchitecture.Infrastructure.Models;
using CleanArchitecture.Infrastructure.Models.Business.Columns;
using CleanArchitecture.Infrastructure.Models.SystemManage;
using CleanArchitecture.Infrastructure.Services.Security;
using CleanArchitecture.Web.Backend.BackendApi;
using CleanArchitecture.Web.Factories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.Differencing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CleanArchitecture.Web.Controllers.Backend.BackendApi
{
    public class SystemManageApiController : SystemManageController
    {
        private readonly ISystemManageModelFactory _systemmanageFactory;
        private readonly ISystemDataDicService _systemdatadicService;
        private readonly ISystemModuleService _systemModuleService;
        private readonly IDomainEventDispatcher _eventDispatcher;
        private readonly IPermissionService _permissionService;
        private readonly IAdminUserService _adminuserService;
        private readonly ISettingService _settingService;

        public SystemManageApiController(ISystemDataDicService systemdatadicService,
            ISystemManageModelFactory systemmanageFactory,
            ISystemDataDicService _systemdatadicService,
            ISystemModuleService systemmoduleService,
            IDomainEventDispatcher eventDispatcher,
            IPermissionService permissionService,
            IAdminUserService adminuserService,
            ISettingService settingService)
            : base(systemdatadicService,
                  systemmanageFactory,
                  systemmoduleService,
                  permissionService,
                  adminuserService,
                  settingService)
        {
            this._systemdatadicService = systemdatadicService;
            this._systemmanageFactory = systemmanageFactory;
            this._systemModuleService = systemmoduleService;
            this._permissionService = permissionService;
            this._adminuserService = adminuserService;
            this._eventDispatcher = eventDispatcher;
            this._settingService = settingService;
        }

        #region SystemDataDic
        [Route(BackendApiRouteConfig.LoadSystemDataDicTree)]
        public async Task<IActionResult> LoadSystemDataDicTree(int grouptype)
        {
            try
            {
                var ztrees = _systemmanageFactory.PrepareSystemDataDicTreeNodes(grouptype);
                return SuccessJson("", ztrees);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }
        /// <summary>
        /// 为机构组织设置排序号
        /// </summary>
        /// <param name="organizeid"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        [HttpGet(BackendApiRouteConfig.BackendOrganizeSetOrder)]
        public async Task<IActionResult> BackendOrganizeSetOrder(int organizeid, int order = 0)
        {
            try
            {
                if (order <= 0) throw new NowaException(ApiResponseMessages.ArgumentCannotBeNull);
                var organizeEntity = _systemdatadicService.GetDataDicTreeById(organizeid);
                if (organizeEntity == null) throw new NowaException(ApiResponseMessages.EntityNotFound);
                organizeEntity.DisplayOrder = order;
                _systemdatadicService.UpdateSystemDataDicTree(organizeEntity);
                return SuccessJson(ApiResponseMessages.DisplayOrderSetSuccess, organizeEntity.DisplayOrder);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }
        [HttpPost(BackendApiRouteConfig.BackendOrganizeUpdate)]
        public async Task<IActionResult> BackendOrganizeUpdate([FromBody] SystemDataDicTreeItemModel organize)
        {
            try
            {
                if (organize == null) throw new NowaException(ApiResponseMessages.ModelBindingError);
                else if (string.IsNullOrEmpty(organize.DataDicTreeName)) throw new NowaException(ApiResponseMessages.ArgumentCannotBeNull);
                if (organize.Id == organize.ParentId)
                    throw new NowaException(ApiResponseMessages.ParentNodeCannotbeIteself);
                if (string.IsNullOrEmpty(organize.DisplayName)) organize.DisplayName = organize.DataDicTreeName;

                organize.GroupType = EntityEnumConfig.DataDicGroup.Oragnize;
                organize.IsActive = EntityEnumConfig.YesorNo.YES;
                var organizeEntity = organize.ToEntity<SystemDataDicTree>();
                if (organize.Id > 0)
                    _systemdatadicService.UpdateSystemDataDicTree(organizeEntity);
                else
                {
                    organizeEntity.CreatedOnTimeUtc = DateTime.UtcNow;
                    _systemdatadicService.InsertSystemDataDicTree(organizeEntity);
                }
                return SuccessJson(ApiResponseMessages.UpdateSuccessAndRefreshCache, organizeEntity.DisplayOrder);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }
        [HttpPost(BackendApiRouteConfig.BackendOrganizeDelete)]
        public async Task<IActionResult> BackendOrganizeDelete([FromBody] DeleteNodeModel node)
        {
            try
            {
                if (!ModelState.IsValid || node == null) throw new NowaException(ApiResponseMessages.ModelBindingError);
                if (!_permissionService.AuthorizeModuleButton(node.ModuleId, node.ButtonId))
                    throw new NowaException(ApiResponseMessages.PermissionNotAuthorize);
                //检查指定删除机构下是否有子节点
                var organizes = _systemdatadicService.GetDataDicTreeByGroupType(EntityEnumConfig.DataDicGroup.Oragnize);
                if (organizes.Any(o => o.ParentId == node.EntityId))
                    throw new NowaException(ApiResponseMessages.DeleteNodeHasChildNodes);
                //与用户角色关联组织机构不能挂载待删除组织机构ID
                var roleModels = _adminuserService.GetAdminRoleModelsAll();
                if (roleModels.Any(r => r.FKOrganizeId == node.EntityId))
                    throw new NowaException(ApiResponseMessages.OrganizeIdExsitsInRoleModel);
                var deleteEntity = _systemdatadicService.GetDataDicTreeById(node.EntityId);
                if (deleteEntity == null)
                    throw new NowaException(ApiResponseMessages.EntityNotFound);
                _systemdatadicService.DeleteSystemDataDicTree(deleteEntity);
                return SuccessJson(ApiResponseMessages.UpdateSuccessAndRefreshCache);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }
        [HttpPost(BackendApiRouteConfig.LoadSystemDataDicButtons)]
        public async Task<IActionResult> LoadSystemDataDicButtons([FromBody] SystemDataDicTreeSearchModel searchModel)
        {
            try
            {
                var buttonModles = _systemmanageFactory.PrepareSystemDataDicButtonListModel(searchModel);
                return SuccessJson("", buttonModles);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }
        #endregion

        #region SystemModule
        [Route(BackendApiRouteConfig.LoadSystemModuleTreeAll)]
        public async Task<IActionResult> LoadSystemModuleTreeAll()
        {
            try
            {
                var ztrees = _systemmanageFactory.PrepareSystemModuleTreeNodes();
                return SuccessJson("", ztrees);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }
        [Route(BackendApiRouteConfig.LoadSystemModuleTreeById)]
        public async Task<IActionResult> LoadSystemModuleTreeById(int id = 0)
        {
            try
            {
                var models = _systemmanageFactory.GetSystemModuleModelsById(id);
                return SuccessJson("", models);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }
        [HttpPost(BackendApiRouteConfig.UpdateSystemModule)]
        public async Task<IActionResult> EditSystemModule([FromBody] SystemModuleModel editModel)
        {
            try
            {
                if (!ModelState.IsValid || editModel == null) throw new NowaException(ApiResponseMessages.ModelBindingError);
                editModel.UrlRoute = editModel.UrlRoute.ToLower();
                if (!editModel.UrlRoute.StartsWith('/') && editModel.ModuleType == EntityEnumConfig.SysModuleType.ModulePage)
                    editModel.UrlRoute = "/" + editModel.UrlRoute;
                else if (editModel.ModuleType != EntityEnumConfig.SysModuleType.ModulePage) editModel.UrlRoute = null;
                if (editModel.ModuleType == EntityEnumConfig.SysModuleType.ModulePage)
                {
                    if (!CommonHelper.IsValidURI(editModel.UrlRoute, UriKind.Relative))
                        throw new NowaException(ApiResponseMessages.UrlNotCorrect);
                }
                var checkUrlUnique = _systemModuleService.GetSystemModuleByUri(editModel.UrlRoute);
                if (editModel.Id > 0 && checkUrlUnique == null) checkUrlUnique = _systemModuleService.GetSystemModuleById(editModel.Id);
                if ((editModel.Id == 0 && editModel.ParentId == 0)
                  || (editModel.ParentId == 0 && editModel.ModuleType != EntityEnumConfig.SysModuleType.Root))
                    throw new NowaException(ApiResponseMessages.RootNodeCannotChange);
                var parentModule = _systemModuleService.GetSystemModuleById(editModel.ParentId);
                if (parentModule == null || parentModule.ModuleType == EntityEnumConfig.SysModuleType.ModulePage)
                    throw new NowaException(ApiResponseMessages.ModulePageCanntbeParent);
                if (string.IsNullOrEmpty(editModel.ModuleName)) throw new NowaException(ApiResponseMessages.ArgumentCannotBeNull);
                else editModel.ModuleName = CommonHelper.CamelCase(editModel.ModuleName);
                if (string.IsNullOrEmpty(editModel.DisplayName)) editModel.DisplayName = editModel.ModuleName;
                if (!string.IsNullOrEmpty(editModel.UrlRoute) && editModel.ModuleType == EntityEnumConfig.SysModuleType.ModulePage)
                {
                    if (editModel.Id == 0 && checkUrlUnique != null)  //新增时对url唯一性检查
                        throw new NowaException(ApiResponseMessages.EnsureUrlUnique);
                    else if (editModel.Id > 0)  //修改时url不能重复
                    {
                        if (editModel.Id != checkUrlUnique.Id)
                            throw new NowaException(ApiResponseMessages.EnsureUrlUnique);
                    }
                }
                else if (string.IsNullOrEmpty(editModel.UrlRoute) && editModel.ModuleType == EntityEnumConfig.SysModuleType.ModulePage)
                    throw new NowaException(ApiResponseMessages.UrlNotNullWhenModulePage);
                var roles = _adminuserService.GetAdminRoleModelsAll();
                var editEntity = editModel.ToEntity<SystemModule>();
                if (editModel.ModuleAssignedButtons != null && editModel.ModuleAssignedButtons.Count > 0)
                {
                    editModel.ModuleAssignedButtons.Sort((x, y) => x.CompareTo(y));
                    editEntity.ModuleAssignedButtonIds = string.Join(',', editModel.ModuleAssignedButtons.ToArray());
                }
                if (editModel.Id == 0)
                {
                    //新增系统模块时，对所有角色分配模块权限
                    foreach (var role in roles)
                    {
                        var mapping = new AdminRoleSystemModuleMapping();
                        if (role.IsAdmin == EntityEnumConfig.YesorNo.YES)
                        {//系统管理员自动对模块及对模块分配的按钮拥有使用权限
                            mapping.IsAllow = EntityEnumConfig.YesorNo.YES;
                            mapping.AuthorizedButtonIds = editEntity.ModuleAssignedButtonIds;
                        }
                        else
                        {
                            mapping.IsAllow = EntityEnumConfig.YesorNo.No;
                            mapping.AuthorizedButtonIds = null;
                        }
                        mapping.FKAdminRoleId = role.Id;
                        editEntity.AdminroleSystemModuleMappings.Add(mapping);
                    }
                    editEntity.CreatedOnTimeUtc = DateTime.UtcNow;
                    _systemModuleService.InsertSystemModule(editEntity);
                }
                else
                {
                    //修改系统模块时，如果分配给模块的按钮发生改变，查询是否被取消分配的按钮在角色模块映身表中做过授权
                    //如果授权则应先在权限分配中取消对按钮授权，再取消按钮分配给模块
                    var mappings = _systemModuleService.GetAdminRoleSystemModuleMappingsByRoleIds(roles.Select(r => r.Id).ToArray());
                    foreach (var mapping in mappings.Where(m => m.FKSystemModuleId == editEntity.Id).ToList())
                    {
                        if (!string.IsNullOrEmpty(mapping.AuthorizedButtonIds))
                        {
                            if (editModel.ModuleAssignedButtons == null || editModel.ModuleAssignedButtons.Count == 0)
                                throw new NowaException(ApiResponseMessages.CancelModuleEdit);
                            var authorizedBtns = mapping.AuthorizedButtonIds.Split(",").Select(Int32.Parse).ToArray();
                            if (authorizedBtns.Any(btnid => !editModel.ModuleAssignedButtons.Contains(btnid)))
                                throw new NowaException(ApiResponseMessages.CancelModuleEdit);
                        }
                    }
                    editEntity.CreatedOnTimeUtc = checkUrlUnique.CreatedOnTimeUtc;
                    _systemModuleService.UpdateSystemModule(editEntity);
                }

                //if ((editModel.Id == 0 && editModel.ModuleAssignedButtons != null && editModel.ModuleAssignedButtons.Count > 0)
                //|| (editModel.ModuleAssignedButtons == null && checkUrlUnique.ModuleAssignedButtons != null && checkUrlUnique.ModuleAssignedButtons.Count > 0)
                //|| (editModel.ModuleAssignedButtons != null && checkUrlUnique.ModuleAssignedButtons != null && !CommonHelper.ArraysEqual(editModel.ModuleAssignedButtons.ToArray(), checkUrlUnique.ModuleAssignedButtons.ToArray())))
                _eventDispatcher.Dispatch(new SystemModuleAssignedButtonsChangedEvent(editEntity));
                return SuccessJson(ApiResponseMessages.UpdateSuccessAndRefreshCache);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }

        [HttpDelete(BackendApiRouteConfig.DeleteSystemModule)]
        public async Task<IActionResult> DeleteSystemModule([FromBody] DeleteNodeModel node)
        {
            try
            {
                if (!ModelState.IsValid || node == null) throw new NowaException(ApiResponseMessages.ModelBindingError);
                var moduleNodes = _systemModuleService.GetSystemMenusAll();
                var deleteNode = moduleNodes.FirstOrDefault(m => m.Id == node.EntityId);
                if (deleteNode == null || deleteNode.IsActive == EntityEnumConfig.YesorNo.No)
                    throw new NowaException(ApiResponseMessages.EntityNotFound);
                if (deleteNode != null && moduleNodes.Any(m => m.ParentId == deleteNode.Id))
                    throw new NowaException(ApiResponseMessages.DeleteNodeHasChildNodes);
                //deleteNode.SysLogLvl = Core.Configuration.SystemLogLevel.Information;
                //deleteNode.SysLogMsg = string.Format("模块Id:{0}已删除，操作者:{1}", deleteNode.Id, _workContext.CurrentAdminUser.UserName);
                _systemModuleService.DeleteSystemModule(deleteNode);
                if (deleteNode.ModuleAssignedButtons != null && deleteNode.ModuleAssignedButtons.Count > 0)
                    _eventDispatcher.Dispatch(new SystemModuleAssignedButtonsChangedEvent(deleteNode));
                return SuccessJson(ApiResponseMessages.UpdateSuccessAndRefreshCache);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }

        [Route(BackendApiRouteConfig.LoadSystemModuleAuthorizeDataByRoleId)]
        public async Task<IActionResult> LoadSystemModuleAuthorizeDataByRoleId(int rid)
        {
            try
            {
                var roleModel = _adminuserService.GetAdminRoleModelsAll().FirstOrDefault(r => r.Id == rid);
                if (roleModel == null) throw new NowaException(ApiResponseMessages.AdminRoleNotExsitsError);
                var role = roleModel.ToEntity<AdminRole>();
                role.Organize = null;
                var models = _permissionService.GetAuthorizedSystemModulesByAdminRoleId(role);
                return SuccessJson("", models);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }
        [HttpPost(BackendApiRouteConfig.UpdateSystemModuleAuthorize)]
        public async Task<IActionResult> UpdateSystemModuleAuthorize([FromQuery] int rid, [FromBody] IList<SystemModuleModel> authorizeList)
        {
            try
            {
                if (rid <= 0 || authorizeList == null || authorizeList.Count == 0)
                    throw new NowaException(ApiResponseMessages.ArgumentCannotBeNull);
                var roleModel = _adminuserService.GetAdminRoleModelsAll().FirstOrDefault(r => r.Id == rid);
                if (roleModel == null) throw new NowaException(ApiResponseMessages.AdminRoleNotExsitsError);
                var role = roleModel.ToEntity<AdminRole>();
                role.Organize = null;
                if (roleModel.IsAdmin == EntityEnumConfig.YesorNo.YES)
                {//系统管理员无论前端如何修改都全量保存所有权限
                    authorizeList.Clear();
                    authorizeList = _permissionService.GetAuthorizedSystemModulesByAdminRoleId(role);
                }
                var updateList = new List<AdminRoleSystemModuleMapping>();
                foreach (var au in authorizeList)
                {
                    var mapping = new AdminRoleSystemModuleMapping();
                    //参见GetAuthorizedSystemModulesByAdminRoleId()字段借用存储
                    mapping.IsAllow = au.IsActive;
                    mapping.AuthorizedButtonIds = au.ModuleAuthorizedButtons == null
                                                ? string.Empty
                                                : string.Join(",", au.ModuleAuthorizedButtons);
                    mapping.FKSystemModuleId = au.Id;
                    mapping.FKAdminRoleId = role.Id;
                    updateList.Add(mapping);
                }
                _systemModuleService.UpdateAdminRoleAuthorize(updateList);
                return SuccessJson(ApiResponseMessages.UpdateSuccessAndRefreshCache);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }

        [Route(BackendApiRouteConfig.LoadColumAuthorizeData)]
        public async Task<IActionResult> LoadColumAuthorizeData(int rid, int columntype)
        {
            try
            {
                var roleModel = _adminuserService.GetAdminRoleModelsAll().FirstOrDefault(r => r.Id == rid);
                if (roleModel == null) throw new NowaException(ApiResponseMessages.AdminRoleNotExsitsError);
                EntityEnumConfig.ColumnType cType = EntityEnumConfig.ColumnType.NewsColumn; //Default Value
                if (typeof(EntityEnumConfig.ColumnType).IsEnumDefined(columntype))
                    cType = CommonHelper.To<EntityEnumConfig.ColumnType>(columntype);
                else throw new NowaException(ApiResponseMessages.ArgumentCannotBeNull);
                var role = roleModel.ToEntity<AdminRole>();
                role.Organize = null;
                var models = _permissionService.GetColumnAuthorizeData(role, cType);
                return SuccessJson("", models);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }

        [HttpPost(BackendApiRouteConfig.UpdateColumnAuthorize)]
        public async Task<IActionResult> UpdateColumnAuthorize([FromQuery] int rid, [FromQuery] int columntype, [FromBody] IList<ColumnAthorizedModel> authorizeList)
        {
            try
            {
                if (rid <= 0 || columntype <= 0 || authorizeList == null || authorizeList.Count == 0)
                    throw new NowaException(ApiResponseMessages.ArgumentCannotBeNull);
                var roleModel = _adminuserService.GetAdminRoleModelsAll().FirstOrDefault(r => r.Id == rid);
                if (roleModel == null) throw new NowaException(ApiResponseMessages.AdminRoleNotExsitsError);
                EntityEnumConfig.ColumnType cType = EntityEnumConfig.ColumnType.NewsColumn; //Default Value
                if (typeof(EntityEnumConfig.ColumnType).IsEnumDefined(columntype))
                    cType = CommonHelper.To<EntityEnumConfig.ColumnType>(columntype);
                else throw new NowaException(ApiResponseMessages.ArgumentCannotBeNull);
                var role = roleModel.ToEntity<AdminRole>();
                role.Organize = null;
                if (roleModel.IsAdmin == EntityEnumConfig.YesorNo.YES)
                {//系统管理员无论前端如何修改都全量保存所有权限
                    authorizeList.Clear();
                    authorizeList = _permissionService.GetColumnAuthorizeData(role, cType);
                }
                var updateList = new List<AdminRoleBusColumnMapping>();
                foreach (var au in authorizeList)
                {
                    var mapping = new AdminRoleBusColumnMapping()
                    {
                        IsAllow = au.IsAllow ? EntityEnumConfig.YesorNo.YES : EntityEnumConfig.YesorNo.No,
                        AuthorizedButtonIds = au.AuthorizedButtonIds,
                        AssignedButtonIds = au.AssignedButtonIds,
                        FKColumnId = au.Id,
                        FKAdminRoleId = role.Id
                    };
                    updateList.Add(mapping);
                }
                var columnService = EngineContext.Current.Resolve<IColumnService>();
                columnService.UpdateAdminRoleColumnAuthorize(updateList);
                return SuccessJson(ApiResponseMessages.UpdateSuccessAndRefreshCache);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }
        #endregion

        #region WebsiteManage
        /// <summary>
        /// WebsiteManage 页面采用iframe方式，故先检查按钮权限再提交保存
        /// </summary>
        /// <param name="btnid"></param>
        /// <returns></returns>
        [Route(BackendApiRouteConfig.CheckWebsiteManageButton)]
        [HttpGet]
        public async Task<IActionResult> CheckWebsiteManageButton(int moduleid, int buttonid)
        {
            try
            {
                if (moduleid <= 0 || buttonid <= 0) throw new NowaException(ApiResponseMessages.ArgumentCannotBeNull);
                if (!_permissionService.AuthorizeModuleButton(moduleid, buttonid))
                    throw new NowaException(ApiResponseMessages.PermissionNotAuthorize);
                return SuccessJson("");
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }
        [HttpPost(BackendApiRouteConfig.WebsiteManageEdit)]
        public async Task<IActionResult> WebsiteManageEdit([FromQuery] int datatype, [FromBody] ClientSiteSettings modeledit)
        {
            try
            {
                if (!ModelState.IsValid || modeledit == null) throw new ArgumentNullException(ApiResponseMessages.ModelBindingError);
                EntityEnumConfig.DataType result = typeof(EntityEnumConfig.DataType).IsEnumDefined(datatype) && datatype >= 100 //过滤非Client(站点)客户端类型
                                             ? (EntityEnumConfig.DataType)datatype
                                             : EntityEnumConfig.DataType.ClientPC; //Default
                if (!string.IsNullOrEmpty(modeledit.CompanyInfo_MapPoint))
                {//对坐标地址有效性校验
                    var point = modeledit.CompanyInfo_MapPoint.Split(",");
                    if (point.Length < 2) modeledit.CompanyInfo_MapPoint = "";
                    else modeledit.CompanyInfo_MapPoint = string.Join(',', point.Select(p => p.Trim()));
                }
                if (result == EntityEnumConfig.DataType.ClientPC)
                {
                    ClientSitePCSettings pcSetting = new ClientSitePCSettings(modeledit);
                    _settingService.SaveSetting(pcSetting);
                }
                else if (result == EntityEnumConfig.DataType.ClientMobile)
                {
                    ClientSiteMobileSettings mobileSetting = new ClientSiteMobileSettings(modeledit);
                    _settingService.SaveSetting(mobileSetting);
                }
                return SuccessJson(ApiResponseMessages.SettingEditSuccess);
            }
            catch (Exception ex)
            {
                return ErrorJson(ex.Message);
            }
        }
        #endregion
    }
}